<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    div{
        background-color: #23d1e4;
        width: 1500px;
        height: 750px;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    form{
        width: 500px;
        height: 400px;
        padding: 50px 25px;
        background-color: #f5f5f5;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        justify-content: space-evenly;
        align-items: center;
    }
    input{
        display: block;
        background-color: #fff;
        color: black;
        width: 300px;
        height: 30px;
    }
    input[type=submit]{
        background-color: blue;
        color:white;
        border:none;
        cursor: pointer;
    }
    form>div{
        height: 16px;
        width: 350px;
        padding-inline-end:55px;
        background-color: #f5f5f5;
        display: flex;
        justify-content: end;
    }
    a{
        text-decoration: none;
        justify-self:end;
        color: black;
    }
</style>
<body>
    <div>
        <form>
            <input type="text" name="username" placeholder="账号">
            <input type="text" name="password" placeholder="密码">
            <input type="submit" value="登录">
            <div>
                <a href="register.html">去注册</a>
            </div>
        </form>
    </div>    
</body>
</html>
<script src="./axios.js"></script>
<script>

    var oTxt1 = document.querySelector("[name=username]")
    var oTxt2 = document.querySelector("[name=password]")
    var oForm = document.querySelector("form");
    var oSpan = document.querySelector("span");
    oForm.onsubmit = function(ev) {   //ev:事件对象
        //1.阻止表单的默认跳转行为
        ev.preventDefault();
        //2.收集用户信息
        var username = oTxt1.value
        var password = oTxt2.value
        // 3.非空校验(如果有一个是空的，就提示填写完成的信息，终止程序继续向下执行)
        if(!username || !password) return alert("请填写完成的信息")
        // 4.发送axios请求
        axios({
            method:"post",
            url:"http://localhost:8888/users/login",
            headers:{
                "content-type":"application/x-www-form-urlencoded"
            },
            data:{
                username,
                password,
            }
        }).then(res => {
            console.log(res);
            //根据状态码判定注册是否成功 和 失败
            if(res.data.code != 1){
                oSpan.innerText = '亲，登录失败，请重新登录'
                oSpan.style.color = 'red';
                //终止继续向下执行
                return;
            }
            // 如果能执行到这里，说明code=1
            //把生成的token和id存储一下(为什么要存？首页需要查看你是否登录，token就是登录凭证)
            localStorage.setItem("token",res.data.token)
            localStorage.setItem("id",res.data.user.id)
            alert("登陆成功");
            //跳转到登录
            location.href = 'index.html'
        })
    }
</script>